﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace 玩具商城管理系统.Order
{
    public partial class Delete_order : Form
    {
        public Delete_order()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProudct, dalog;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from orderT";
            string sdr = "select * from log";
            daProudct = new SqlDataAdapter(str, DB.cn);
            dalog = new SqlDataAdapter(sdr, DB.cn);
            daProudct.Fill(ds, "order_info");
            dalog.Fill(ds, "log_info");
        }
        void showAll()
        {
            DataView dvorder = new DataView(ds.Tables["order_info"]);
            dgvorder.DataSource = dvorder;
            this.dgvorder.Columns[1].HeaderText = "订单编号";
            this.dgvorder.Columns[2].HeaderText = "用户名称";
            this.dgvorder.Columns[3].HeaderText = "下单时间";
            this.dgvorder.Columns[4].HeaderText = "用户地址";
            this.dgvorder.Columns[5].HeaderText = "用户省份";
            this.dgvorder.Columns[6].HeaderText = "用户城市";
            this.dgvorder.Columns[7].HeaderText = "用户地区";
            this.dgvorder.Columns[8].HeaderText = "地区邮编";
            this.dgvorder.Columns[9].HeaderText = "审核状态";
            this.dgvorder.Columns[10].HeaderText = "审核人员";
            this.dgvorder.Columns[11].HeaderText = "订单收益";
            this.dgvorder.Columns[12].HeaderText = "用户备注";
        }

        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProudct);
                daProudct.Update(ds, "order_info");
                SqlCommandBuilder dblog = new SqlCommandBuilder(dalog);
                dalog.Update(ds, "log_info");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            DB.cn.Close();
        }
        private void dgvorder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvorder.Rows.Count > 0)
            {
                for (int i = 0; i < dgvorder.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvorder.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int s = dgvorder.Rows.Count;
            for (int i = 0; i < dgvorder.Rows.Count; i++)
            {
                if (dgvorder.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    DialogResult dr = MessageBox.Show("您确定要对此商品进行删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        
                        DB.GetCn();
                        string result = dgvorder.Rows[i].Cells["orderT_id"].Value.ToString();
                        string pro = "";
                        pro = "delete from hot where  orderT_id = '" + result + "'";
                        DB.sqlEx(pro);
                        dgvorder.Rows.RemoveAt(i);
                        //添加日志
                        DataRow drlog = ds.Tables["log_info"].NewRow();
                        drlog["username"] = login.username;
                        drlog["type"] = "删除";
                        drlog["action_date"] = DateTime.Now;
                        drlog["action_table"] = "orderT表";
                        ds.Tables["log_info"].Rows.Add(drlog);

                        UpdateDB();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    s = s - 1;

                }
            }
            if (s == 0)
            {
                MessageBox.Show("请选择你要删除的商品");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Delete_order_Load(object sender, EventArgs e)
        {
            if (login.Dflag == false)
            {
                MessageBox.Show("需要以销售部员工身份登录才可以进入");
                this.Close();
            }
            else
            {
                showXz();
                init();
                showAll();
            }
        }

        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvorder.Columns.Add(acCode);

        }

}
}
